package lab2Exercise3;

public class Calculator {
	
	public static int calculate(String[] arr)
	{
		ArrayStack<String> stack = new ArrayStack<String>();
		for (String item : arr) {
			stack.push(item);
		}
		
		for (int i = 0; i < arr.length; i++) {
			arr[i] = stack.pop();
		}
		for (String item: arr) {
			if(isNumber(item)) {
				stack.push(item);
			}
			else if(!isNumber(item)) {
				stack.push(applyOp(stack.pop(), item, stack.pop()));
			}
		}

		return Integer.parseInt(stack.top());
		
	}
	
	public static int converter(String str)
	{
		return Integer.parseInt(str);
	}
	
	public static boolean isNumber(String str)
	{
		try {
			Integer.parseInt(str);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
		
		
	}
	
	public static String applyOp(String left, String op, String right)
	{
		if(op.equals("+")) {
			return Integer.toString(converter(left) + converter(right));
		}
		else if(op.equals("-")) {
			return Integer.toString(converter(left) - converter(right));
		}
		return "";
	}
	public static void reverse (String[] arr)
	{
		ArrayStack<String> stack1 = new ArrayStack<String>(arr.length);
		for(String item : arr) {
			stack1.push(item);
		}
		for (int i = 0; i < arr.length;i ++) {
			arr[i] = stack1.pop();
		}

	}

}
